package com.ljc.config;

import com.ljc.constant.RabbitMQConstant;
import com.ljc.utils.DanmuRabbitMQUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Configuration
public class RabbitMQConfig {
    // 手动触发ConnectionListener 便可解决通过@Bean创建队列不生效的问题
    @Bean
    ApplicationRunner runner(ConnectionFactory cf) {
        log.info("执行成功");
        return args -> cf.createConnection().close();
    }

    /**
     * 普通队列
     */
    @Bean
    public Queue movementQueue() {
        Map<String, Object> arguments = new HashMap<>();
        //声明死信队列和交换机消息，过期时间：1分钟
        arguments.put("x-dead-letter-exchange", RabbitMQConstant.MOVEMENT_EXCHANGE_NAME);
        arguments.put("x-dead-letter-routing-key", RabbitMQConstant.MOVEMENT_DELAY_EXCHANGE_ROUTING_KEY);
        arguments.put("x-message-ttl", 60000);
        return new Queue(RabbitMQConstant.MOVEMENT_QUEUE_NAME, true, false, false, arguments);
    }

    /**
     * 死信队列：消息重试三次后放入死信队列
     */
    @Bean
    public Queue movementDeadLetter() {
        return new Queue(RabbitMQConstant.MOVEMENT_DELAY_QUEUE_NAME, true, false, false);
    }

    /**
     * 主题交换机
     */
    @Bean
    public Exchange movementExchange() {
        return new TopicExchange(RabbitMQConstant.MOVEMENT_EXCHANGE_NAME, true, false);
    }


    /**
     * 交换机和普通队列绑定
     */
    @Bean
    public Binding movementBinding() {
        return new Binding(RabbitMQConstant.MOVEMENT_QUEUE_NAME, Binding.DestinationType.QUEUE,
                RabbitMQConstant.MOVEMENT_EXCHANGE_NAME, RabbitMQConstant.MOVEMENT_EXCHANGE_ROUTING_KEY, null);
    }

    /**
     * 交换机和死信队列绑定
     */
    @Bean
    public Binding movementDelayBinding() {
        return new Binding(RabbitMQConstant.MOVEMENT_DELAY_QUEUE_NAME, Binding.DestinationType.QUEUE,
                RabbitMQConstant.MOVEMENT_EXCHANGE_NAME, RabbitMQConstant.MOVEMENT_DELAY_EXCHANGE_ROUTING_KEY, null);
    }

    /**
     * 普通队列
     */
    @Bean
    public Queue danmuQueue() {
        return new Queue(RabbitMQConstant.DANMU_QUEUE_NAME, true, false, false);
    }

    /**
     * 主题交换机
     */
    @Bean
    public Exchange danmuExchange() {
        return new TopicExchange(RabbitMQConstant.DANMU_EXCHANGE_NAME, true, false);
    }

    /**
     * 交换机和普通队列绑定
     */
    @Bean
    public Binding danmuBinding() {
        return new Binding(RabbitMQConstant.DANMU_QUEUE_NAME, Binding.DestinationType.QUEUE,
                RabbitMQConstant.DANMU_EXCHANGE_NAME, RabbitMQConstant.DANMU_EXCHANGE_ROUTING_KEY, null);
    }

    @Bean
    public DanmuRabbitMQUtils getDanmuRabbitMQUtils() {
        return new DanmuRabbitMQUtils();
    }
}
